package com.ccp.dev.qbdms.controller.testhttpclient;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.DictionaryService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassDescribe:
 *
 * @author :milihao
 * @date :2019-04-23 Since:1
 */
@RestController
public class TestController {

	@Autowired
	private AsyncTask asyncTask;

	@RequestMapping("/test/httpClient/test")
	public String submit(int number) {
		asyncTask.aa();
		return "success";
	}

	@Service
	class AsyncTask extends BaseService<SysUser> {

		@Resource
		private DictionaryService dictionaryService;
		@Resource
		private AsyncTaskAb asyncTaskAb;

		@Async("reportAsync")
		public void aa() {
			Dictionary dictionary = new Dictionary();
			dictionary.setDicId("mlh1");
			dictionary.setTypeId("mlh1");
			dictionaryService.add(dictionary);
			asyncTaskAb.bb();
			asyncTaskAb.cc();
		}

		@Async("reportAsync")
		public void bb() {
			long start = System.currentTimeMillis();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Dictionary dictionary = new Dictionary();
			dictionary.setDicId("mlh2");
			dictionary.setTypeId("mlh2");
			dictionaryService.add(dictionary);
			long end = System.currentTimeMillis();
			LOGGER.info("bb finished, time elapsed: {} ms.", end - start);
		}

		@Transactional
		@Async("reportAsync")
		public void cc() {
			long start = System.currentTimeMillis();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Dictionary dictionary = new Dictionary();
			dictionary.setDicId("mlh3");
			dictionary.setTypeId("mlh3");
			dictionaryService.add(dictionary);
			long end = System.currentTimeMillis();
			LOGGER.info("cc finished, time elapsed: {} ms.", end - start);
			throw new RuntimeException("cc抛出运行异常");
		}

		protected final Logger LOGGER = LoggerFactory.getLogger(AsyncTask.class);

		@Async("reportAsync")
		public Future<String> doTask1(int number) throws InterruptedException {
			long start = System.currentTimeMillis();
			Thread.sleep(number);
			long end = System.currentTimeMillis();
			LOGGER.info("Task1 finished, time elapsed: {} ms.", end - start);
			return new AsyncResult<>("Task1 accomplished!");
		}
	}

	@Service
	class AsyncTaskAb extends BaseService<SysUser> {

		@Resource
		private DictionaryService dictionaryService;

		@Async("reportAsync")
		public void bb() {
			long start = System.currentTimeMillis();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Dictionary dictionary = new Dictionary();
			dictionary.setDicId("mlh2");
			dictionary.setTypeId("mlh2");
			dictionaryService.add(dictionary);
			long end = System.currentTimeMillis();
			LOGGER.info("bb finished, time elapsed: {} ms.", end - start);
		}

		@Transactional
		@Async("reportAsync")
		public void cc() {
			long start = System.currentTimeMillis();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Dictionary dictionary = new Dictionary();
			dictionary.setDicId("mlh3");
			dictionary.setTypeId("mlh3");
			dictionaryService.add(dictionary);
			long end = System.currentTimeMillis();
			LOGGER.info("cc finished, time elapsed: {} ms.", end - start);
			throw new RuntimeException("cc抛出运行异常");
		}

		protected final Logger LOGGER = LoggerFactory.getLogger(AsyncTask.class);

		@Async("reportAsync")
		public Future<String> doTask1(int number) throws InterruptedException {
			long start = System.currentTimeMillis();
			Thread.sleep(number);
			long end = System.currentTimeMillis();
			LOGGER.info("Task1 finished, time elapsed: {} ms.", end - start);
			return new AsyncResult<>("Task1 accomplished!");
		}
	}
}
